﻿   
 
using System;
using AutoMapper;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;

using Oliving.Edge.AuthHelper; 
using Oliving.Edge.Model;
using Oliving.Edge.Model.Models; 
using Oliving.Edge.IServices;
using Oliving.Edge.Model.Dto;

namespace Oliving.Edge.Controllers 
{
    [ApiController]
    [Route("api/DTSN_WuAPI_202004")]
    [Authorize(Permissions.Name)]
    public class DTSN_WuAPI_202004Controller : OlivingControll 
    {
            IMapper _mapper;
            private readonly IDTSN_WuAPI_202004Services _DTSN_WuAPI_202004Services;
            private readonly ILogger<DTSN_WuAPI_202004Controller> _logger;


            public DTSN_WuAPI_202004Controller(
                    IHttpContextAccessor accessor, PermissionRequirement requirement,
                    IMapper mapper, ILogger<DTSN_WuAPI_202004Controller> logger,
                    IDTSN_WuAPI_202004Services DTSN_WuAPI_202004Services
                ) : base(accessor, requirement)
            {
                _logger = logger;
                _mapper = mapper;
                _DTSN_WuAPI_202004Services = DTSN_WuAPI_202004Services;
            }

 

            [HttpGet]
            [AllowAnonymous]
            [Route("/api/[controller]/Get")]
            public async Task<MessageModel<DTSN_WuAPI_202004Dto>> Get(int id)
            {
                var data = new MessageModel<DTSN_WuAPI_202004Dto>();
                DTSN_WuAPI_202004 model = await _DTSN_WuAPI_202004Services.QueryById(id);
                if (model == null)
                {
                    data.response = null;
                    data.msg = "数据不存在！";
                    data.success = false;
                }
                else
                {
                    var dto = _mapper.Map<DTSN_WuAPI_202004Dto>(model);
                    data.response = dto;
                    data.msg = "获取成功！";
                    data.success = true;
                } 
                return data;
            } 





            [HttpGet]
            [Route("/api/[controller]/GetPageList")]
            public async Task<MessageModel<PageModel<DTSN_WuAPI_202004Dto>>> GetPageList(int page = 1, int intPageSize = 20, string key = "")
            { 
                if (string.IsNullOrEmpty(key) || string.IsNullOrWhiteSpace(key))
                {
                    key = "";
                }

                var data = await _DTSN_WuAPI_202004Services.QueryPage(a =>
                                true
                                , page, intPageSize, " WL_ID desc ");

                var model = _mapper.Map<PageModel<DTSN_WuAPI_202004Dto>>(data);

                return new MessageModel<PageModel<DTSN_WuAPI_202004Dto>>()
                {
                    msg = "获取成功",
                    success = true,
                    response = model
                };
            } 
            

 
                [HttpPut]
                [Route("/api/[controller]/Update")]
                public async Task<MessageModel<string>> Update([FromBody] DTSN_WuAPI_202004Dto dto)
                {
                    var data = new MessageModel<string>();

                    if (dto != null && dto.WL_ID > 0)
                    {
                        DTSN_WuAPI_202004 model = await _DTSN_WuAPI_202004Services.QueryById(dto.WL_ID);
                        if (model != null)
                        {
                               model.WL_ApiURL = dto.WL_ApiURL;  
   model.WL_Parameter = dto.WL_Parameter;  
   model.WL_Result = dto.WL_Result;  
   model.WL_CreateTime = dto.WL_CreateTime;  
   model.WL_InsertTime = dto.WL_InsertTime;  
   model.WL_State = dto.WL_State;  
   model.WL_Remark = dto.WL_Remark;  


                            data.success = await _DTSN_WuAPI_202004Services.Update(model);

                            if (data.success)
                            {
                                data.msg = "更新成功";
                                data.response = dto.WL_ID.ObjToString();
                            }
                        }
                        else
                        {
                            data.success = false;
                            data.msg = "参数异常，您要更新的数据不存在";
                        }
                    }
                    else
                    {
                        data.success = false;
                        data.msg = "参数异常";
                    }
                    return data;
                }

            



                [HttpPost]
                [Route("/api/[controller]/Add")]
                public async Task<MessageModel<string>> Add(DTSN_WuAPI_202004 dto)
                {
                    var data = new MessageModel<string>();
                    int id = await _DTSN_WuAPI_202004Services.Add(dto);

                    data.success = id > 0;
                    if (data.success)
                    {
                        data.response = id.ObjToString();
                        data.msg = "添加成功";
                    }
                    return data;
                } 

            

 
                [HttpDelete]
                [Route("/api/[controller]/Delete")]
                public async Task<MessageModel<string>> Delete(int id)
                {
                    var data = new MessageModel<string>();
                    if (id > 0)
                    {
                        var userDetail = await _DTSN_WuAPI_202004Services.QueryById(id);
                        // userDetail.C_State = -1; ?????????????
                        data.success = await _DTSN_WuAPI_202004Services.Update(userDetail);
                        if (data.success)
                        {
                            data.msg = "删除成功";
                            data.response = userDetail?.WL_ID.ObjToString();
                        }
                    } 
                    return data;
                }

            



            [HttpDelete]
            [Route("/api/[controller]/BatchDelete")]
            public async Task<MessageModel<string>> BatchDelete(string ids)
            {
                var data = new MessageModel<string>();
                if (!string.IsNullOrEmpty(ids))
                {
                    if (false)
                    {
                        #region 软删除

                        int[] list = Array.ConvertAll<string, int>(ids.Split(','), s => int.Parse(s)); 
 
                        DTSN_WuAPI_202004 model = new DTSN_WuAPI_202004();
                        //  model.C_State = -1;          ???????????????????
                        string whereStr = " WL_ID in (" + ids + ")";
                        List<string> colList = new List<string>();
                        //  colList.Add("C_State");    ???????????????????
                        List<string> colHL = new List<string>();
                      
                        bool isOK = await _DTSN_WuAPI_202004Services.Update(model, colList, colHL, whereStr);
 
                        data.success = isOK;
                        if (data.success)
                        {
                            data.msg = "删除成功";
                            data.response = list.Count().ObjToString();
                        }

                        #endregion
                    }
                    {
                        #region 直接删除 

                        object[] list = Array.ConvertAll<string, object>(ids.Split(','), s => int.Parse(s));

                        bool isOK = await _DTSN_WuAPI_202004Services.DeleteByIds(list);

                        data.success = isOK;
                        if (data.success)
                        {
                            data.msg = "删除成功";
                            data.response = list.Count().ObjToString();
                        } 
                        #endregion
                    }
                }
                return data;
            }
 

            

    }
        }
 
